﻿<script lang="ts" name="syssupplier" setup>
import { ref, reactive, onMounted } from "vue";
import { ElMessage } from "element-plus";
import type { FormRules } from "element-plus";
import { formatDate } from '/@/utils/formatTime';
import { generateSupplierCode } from '/@/utils/generateCode';
import { useSyssupplierApi } from '/@/api/interacting/syssupplier';

//父级传递来的函数，用于回调
const emit = defineEmits(["reloadTable"]);
const syssupplierApi = useSyssupplierApi();
const ruleFormRef = ref();

const state = reactive({
	title: '',
	loading: false,
	showDialog: false,
	ruleForm: {} as any,
	stores: {},
	dropdownData: {} as any,
	// 机构列表
	orgList: [] as any[],
	// 用户列表（所有用户）
	allUserList: [] as any[],
	// 过滤后的用户列表（根据选择的部门）
	filteredUserList: [] as any[],
	// 所属行业选项
	industryOptions: [
		'旅游服务',
		'国际贸易',
		'新能源',
		'花卉种植',
		'农业',
		'汽车配件',
		'化工',
		'文化传媒',
		'港口物流',
		'食品加工',
		'建筑工程',
		'医疗器械',
		'物流运输',
		'电子商务',
		'教育',
		'服务业',
		'制造业',
		'贸易',
		'信息技术'
	],
	// 供应商分类选项
	categoryOptions: [
		'次要供应商',
		'一般供应商',
		'主要供应商'
		
	]
});

// 自行添加其他规则
const rules = ref<FormRules>({
  code: [{required: true, message: '请选择供应商编码！', trigger: 'blur',},],
  name: [{required: true, message: '请选择供应商名称！', trigger: 'blur',},],
  status: [{required: true, message: '请选择状态！', trigger: 'blur',},],
});

// 页面加载时
onMounted(async () => {
});

// 打开弹窗
const openDialog = async (row: any, title: string) => {
	state.title = title;
	row = row ?? {  };
	
	if (row.id) {
		// 编辑模式：获取完整数据
		state.ruleForm = await syssupplierApi.detail(row.id).then(res => res.data.result);
	} else {
		// 新增模式：初始化表单数据，自动生成供应商编码
		state.ruleForm = {
			...JSON.parse(JSON.stringify(row)),
			code: generateSupplierCode(), // 供应商编码 - 自动生成
		};
	}
	
	state.showDialog = true;
};

// 关闭弹窗
const closeDialog = () => {
	emit("reloadTable");
	state.showDialog = false;
};

// 提交
const submit = async () => {
	ruleFormRef.value.validate(async (isValid: boolean, fields?: any) => {
		if (isValid) {
			let values = state.ruleForm;
			await syssupplierApi[state.ruleForm.id ? 'update' : 'add'](values);
			closeDialog();
		} else {
			ElMessage({
				message: `表单有${Object.keys(fields).length}处验证失败，请修改后再提交`,
				type: "error",
			});
		}
	});
};

//将属性或者函数暴露给父组件
defineExpose({ openDialog });
</script>
<template>
	<div class="syssupplier-container">
		<el-dialog v-model="state.showDialog" :width="800" draggable :close-on-click-modal="false">
			<template #header>
				<div style="color: #fff">
					<span>{{ state.title }}</span>
				</div>
			</template>
			<el-form :model="state.ruleForm" ref="ruleFormRef" label-width="auto" :rules="rules">
				<el-row :gutter="35">
					<el-form-item v-show="false">
						<el-input v-model="state.ruleForm.id" />
					</el-form-item>
						<el-col :xs="24" :sm="12" :md="12" :lg="12" :xl="12" class="mb20" >
						<el-form-item label="供应商编码" prop="code">
							<el-input 
								v-model="state.ruleForm.code" 
								placeholder="供应商编码" 
								maxlength="50" 
								show-word-limit 
								:readonly="true"
								style="background-color: #f5f7fa;"
							/>
						</el-form-item>
					</el-col>
						<el-col :xs="24" :sm="12" :md="12" :lg="12" :xl="12" class="mb20" >
						<el-form-item label="供应商名称" prop="name">
							<el-input v-model="state.ruleForm.name" placeholder="请输入供应商名称" maxlength="100" show-word-limit clearable />
						</el-form-item>
					</el-col>
						<el-col :xs="24" :sm="12" :md="12" :lg="12" :xl="12" class="mb20" >
						<el-form-item label="所属行业" prop="industry">
							<el-select v-model="state.ruleForm.industry" placeholder="请选择所属行业" clearable filterable>
								<el-option
									v-for="industry in state.industryOptions"
									:key="industry"
									:label="industry"
									:value="industry"
								/>
							</el-select>
						</el-form-item>
					</el-col>
						<el-col :xs="24" :sm="12" :md="12" :lg="12" :xl="12" class="mb20" >
						<el-form-item label="供应商分类" prop="category">
							<el-select v-model="state.ruleForm.category" placeholder="请选择供应商分类" clearable filterable>
								<el-option
									v-for="category in state.categoryOptions"
									:key="category"
									:label="category"
									:value="category"
								/>
							</el-select>
						</el-form-item>
					</el-col>
						<el-col :xs="24" :sm="12" :md="12" :lg="12" :xl="12" class="mb20" >
						<el-form-item label="联系人" prop="contactPerson">
							<el-input v-model="state.ruleForm.contactPerson" placeholder="请输入联系人" maxlength="50" show-word-limit clearable />
						</el-form-item>
					</el-col>
						<el-col :xs="24" :sm="12" :md="12" :lg="12" :xl="12" class="mb20" >
						<el-form-item label="联系电话" prop="contactPhone">
							<el-input v-model="state.ruleForm.contactPhone" placeholder="请输入联系电话" maxlength="20" show-word-limit clearable />
						</el-form-item>
					</el-col>
						<el-col :xs="24" :sm="12" :md="12" :lg="12" :xl="12" class="mb20" >
						<el-form-item label="电子邮箱" prop="email">
							<el-input v-model="state.ruleForm.email" placeholder="请输入电子邮箱" maxlength="100" show-word-limit clearable />
						</el-form-item>
					</el-col>
						<el-col :xs="24" :sm="12" :md="12" :lg="12" :xl="12" class="mb20" >
						<el-form-item label="进货部门Id" prop="purchasingDepartmentId">
							<el-input v-model="state.ruleForm.purchasingDepartmentId" placeholder="请输入进货部门Id" show-word-limit clearable />
						</el-form-item>
					</el-col>
						<el-col :xs="24" :sm="12" :md="12" :lg="12" :xl="12" class="mb20" >
						<el-form-item label="进货部门" prop="purchasingDepartment">
							<el-input v-model="state.ruleForm.purchasingDepartment" placeholder="请输入进货部门" maxlength="50" show-word-limit clearable />
						</el-form-item>
					</el-col>
						<el-col :xs="24" :sm="12" :md="12" :lg="12" :xl="12" class="mb20" >
						<el-form-item label="进货负责人Id" prop="purchasingManagerId">
							<el-input v-model="state.ruleForm.purchasingManagerId" placeholder="请输入进货负责人Id" show-word-limit clearable />
						</el-form-item>
					</el-col>
						<el-col :xs="24" :sm="12" :md="12" :lg="12" :xl="12" class="mb20" >
						<el-form-item label="进货负责人" prop="purchasingManager">
							<el-input v-model="state.ruleForm.purchasingManager" placeholder="请输入进货负责人" maxlength="50" show-word-limit clearable />
						</el-form-item>
					</el-col>
						<el-col :xs="24" :sm="12" :md="12" :lg="12" :xl="12" class="mb20" >
						<el-form-item label="状态" prop="status">
							<el-switch 
								v-model="state.ruleForm.status" 
								:active-value="1" 
								:inactive-value="0"
								active-text="启用" 
								inactive-text="禁用"
							/>
						</el-form-item>
					</el-col>
						<el-col :xs="24" :sm="12" :md="12" :lg="12" :xl="12" class="mb20" >
						<el-form-item label="供应商地址" prop="address">
							<el-input v-model="state.ruleForm.address" placeholder="请输入供应商地址" maxlength="200" show-word-limit clearable />
						</el-form-item>
					</el-col>
						<el-col :xs="24" :sm="12" :md="12" :lg="12" :xl="12" class="mb20" >
						<el-form-item label="备注" prop="remarks">
							<el-input v-model="state.ruleForm.remarks" placeholder="请输入备注" maxlength="255" show-word-limit clearable />
						</el-form-item>
					</el-col>
						<el-col :xs="24" :sm="12" :md="12" :lg="12" :xl="12" class="mb20" >
						<el-form-item label="账户名称" prop="accountName">
							<el-input v-model="state.ruleForm.accountName" placeholder="请输入账户名称" maxlength="100" show-word-limit clearable />
						</el-form-item>
					</el-col>
						<el-col :xs="24" :sm="12" :md="12" :lg="12" :xl="12" class="mb20" >
						<el-form-item label="纳税人识别号" prop="taxpayerId">
							<el-input v-model="state.ruleForm.taxpayerId" placeholder="请输入纳税人识别号" maxlength="50" show-word-limit clearable />
						</el-form-item>
					</el-col>
						<el-col :xs="24" :sm="12" :md="12" :lg="12" :xl="12" class="mb20" >
						<el-form-item label="银行账户" prop="bankAccount">
							<el-input v-model="state.ruleForm.bankAccount" placeholder="请输入银行账户" maxlength="50" show-word-limit clearable />
						</el-form-item>
					</el-col>
						<el-col :xs="24" :sm="12" :md="12" :lg="12" :xl="12" class="mb20" >
						<el-form-item label="开户银行" prop="openingBank">
							<el-input v-model="state.ruleForm.openingBank" placeholder="请输入开户银行" maxlength="100" show-word-limit clearable />
						</el-form-item>
					</el-col>
						<el-col :xs="24" :sm="12" :md="12" :lg="12" :xl="12" class="mb20" >
						<el-form-item label="开户行地址" prop="openingBankAddress">
							<el-input v-model="state.ruleForm.openingBankAddress" placeholder="请输入开户行地址" maxlength="200" show-word-limit clearable />
						</el-form-item>
					</el-col>
						<el-col :xs="24" :sm="12" :md="12" :lg="12" :xl="12" class="mb20" >
						<el-form-item label="软删除时间" prop="deleteTime">
							<el-date-picker v-model="state.ruleForm.deleteTime" type="date" placeholder="软删除时间" />
						</el-form-item>
					</el-col>
				</el-row>
			</el-form>
			<template #footer>
				<span class="dialog-footer">
					<el-button @click="() => state.showDialog = false">取 消</el-button>
					<el-button @click="submit" type="primary" v-reclick="1000">确 定</el-button>
				</span>
			</template>
		</el-dialog>
	</div>
</template>
<style lang="scss" scoped>
:deep(.el-select), :deep(.el-input-number) {
  width: 100%;
}
</style>